﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;

namespace VCN.admin
{
    public partial class AddNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                this.LoadData();
                Page.Title = "Thêm tin tức";

            }
        }
        private void LoadData()
        {
            UsersDAO v_UserDAO = new UsersDAO();
            DataTable v_dt = v_UserDAO.selectAll2Dt();
            ddl_AuthorId.DataSource = v_dt;
            ddl_AuthorId.DataTextField = "USERNAME";
            ddl_AuthorId.DataValueField = "ID";
            ddl_AuthorId.DataBind();
        }

        protected void add_click(object sender, EventArgs e)
        {
            VCN.App.DataTransferObjects.News v_News = new VCN.App.DataTransferObjects.News();
            v_News.NewsImage = this.doUploadPImage();
            v_News.Reference = txt_Reference.Text;
            v_News.ShortContent = txt_ShortContent.Text;
            v_News.Contents = txt_Content.Text;
            v_News.Title = txt_Title.Text;
            v_News.AuthorId = int.Parse(ddl_AuthorId.SelectedItem.Value);
            v_News.CreatedDate = DateTime.Now;
            v_News.ViewCount = 0;
            v_News.Type = int.Parse(txt_Type.Text.ToString());
            NewsDAO v_NewsDAO = new NewsDAO();
            v_NewsDAO.insert(v_News);
            Response.Redirect("NewsList.aspx");
        }

        protected void reset_click(object sender, EventArgs e)
        {
            txt_Title.Text = "";
            txt_ShortContent.Text = "";
            txt_Content.Text = "";
            txt_Reference.Text = "";
            txt_Type.Text = "";
        }

        protected void cancel_click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../home/Images/News/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
            }
            return FileUpload.FileName;
        }     
        
    }
}
